﻿package mortal.game.view.common.drag
{
    import com.gengine.global.*;
    import com.mui.display.*;
    import flash.display.*;
    import flash.events.*;
    import mortal.common.display.*;
    import mortal.game.view.common.*;

    public class DragItem extends Sprite
    {
        private var __width:Number;
        private var __height:Number;
        private var _bmp:ScaleBitmap;
        private var _isDown:Boolean;
        private var _isOver:Boolean;
        private var _isChange:Boolean;
        private var _lastDragX:Number;
        private var _lastDragY:Number;

        public function DragItem(param1:Number = 250, param2:Number = 5)
        {
            this.__width = param1;
            this.__height = param2;
            this._bmp = UIFactory.bg(0, 0, this.__width, this.__height, this, BitmapDataConst.AlphaBMD);
            this.addListeners();
            return;
        }// end function

        public function setSize(param1:Number, param2:Number) : void
        {
            this.__width = param1;
            this.__height = param2;
            this._bmp.setSize(this.__width, this.__height);
            return;
        }// end function

        private function addListeners() : void
        {
            this.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseHandler);
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseHandler);
            this.addEventListener(MouseEvent.MOUSE_OUT, this.onMouseHandler);
            this.addEventListener(Event.ENTER_FRAME, this.onEnterFrameHandler);
            return;
        }// end function

        private function onMouseHandler(event:MouseEvent) : void
        {
            switch(event.type)
            {
                case MouseEvent.MOUSE_OVER:
                {
                    this._isOver = true;
                    this._isChange = true;
                    break;
                }
                case MouseEvent.MOUSE_DOWN:
                {
                    this._isDown = true;
                    this._isChange = true;
                    this._lastDragX = Global.stage.mouseX;
                    this._lastDragY = Global.stage.mouseY;
                    this.dispatchEvent(new DragItemEvent(DragItemEvent.StartDrag));
                    Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseHandler);
                    break;
                }
                case MouseEvent.MOUSE_OUT:
                {
                    this._isOver = false;
                    this._isChange = true;
                    break;
                }
                case MouseEvent.MOUSE_UP:
                {
                    this._isDown = false;
                    this._isChange = true;
                    Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseHandler);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onEnterFrameHandler(event:Event) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            if (this._isDown)
            {
                _loc_2 = Global.stage.mouseX - this._lastDragX;
                _loc_3 = Global.stage.mouseY - this._lastDragY;
                this.dispatchEvent(new DragItemEvent(DragItemEvent.DragPositionChange, _loc_2, _loc_3));
            }
            return;
        }// end function

    }
}
